package org.dolphin.entity.inventory;

import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.ForeignKey;
import org.springframework.util.ObjectUtils;

@Embeddable
public class ProductCategoryLinkPk implements Serializable {
  private static final long serialVersionUID = -7628716307666390877L;
  private Product product;
  private ProductCategory category;

  public ProductCategoryLinkPk() {
  }

  public ProductCategoryLinkPk(Product product, ProductCategory category) {
    setProduct(product);
    setCategory(category);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof ProductCategoryLinkPk) {
      ProductCategoryLinkPk oth = (ProductCategoryLinkPk) obj;
      return ObjectUtils.nullSafeEquals(getProduct() != null ? getProduct().getId() : 0,
          oth.getProduct() != null ? oth.getProduct().getId() : 0)
          && ObjectUtils.nullSafeEquals(getCategory() != null ? getCategory().getId() : 0,
              oth.getCategory() != null ? oth.getCategory().getId() : 0);
    }
    return false;
  }

  @ManyToOne(optional = false)
  @ForeignKey(name = "FK_MS_PRODUCT_TO_CATEGORY_CATEGORY_ID")
  public ProductCategory getCategory() {
    return category;
  }

  @ManyToOne(optional = false)
  @ForeignKey(name = "FK_MS_PRODUCT_TO_CATEGORY_PRODUCT_ID")
  public Product getProduct() {
    return product;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getProduct() != null ? getProduct().getId() : 0L);
    hash ^= ObjectUtils.nullSafeHashCode(getCategory() != null ? getCategory().getId() : 0L);
    return hash;
  }

  public void setCategory(ProductCategory category) {
    this.category = category;
  }

  public void setProduct(Product product) {
    this.product = product;
  }
}
